<template>
    <el-card shadow="never" class="notice">
        <ListHeader @create="handleCreate" @refresh="getData"></ListHeader>
        <el-table :data="tableData" stripe style="width: 100%" v-loading="loading">
            <el-table-column label="优惠券名称" width="350">
                <template #default="{ row }">
                    <div class="couponName" :class="row.statusText == '领取中' ? 'active' : 'inactive'">
                        <h5 class="couponNameH5">{{ row.name }}</h5>
                        <small>{{ row.start_time }} - {{ row.end_time }}</small>
                    </div>
                </template>
            </el-table-column>
            <el-table-column prop="statusText" label="状态" />
            <el-table-column label="优惠" width="120">
                <template #default="{ row }">
                    {{ row.type === 0 ? "满减" : "折扣" }} {{ row.type === 0 ? ("￥" + row.value) : (row.value) + "折" }}
                </template>
            </el-table-column>
            <el-table-column prop="total" label="发放数量" />
            <el-table-column prop="used" label="已使用" />
            <el-table-column label="操作" width="180" align="center">
                <template #default="scope">
                    <el-button v-if="scope.row.statusText === '未开始'" type="primary" size="small" text
                        @click="handleEdit(scope.row)">修改</el-button>
                    <el-popconfirm title="是否删除该优惠券" v-if="scope.row.statusText !== '领取中'" confirmButtonText="确认"
                        cancelButtonText="取消" @confirm.stop="handleDelete(scope.row.id)">
                        <template #reference>
                            <el-button type="primary" size="small" text>删除</el-button>
                        </template>
                    </el-popconfirm>
                    <el-popconfirm v-if="scope.row.statusText === '领取中'" title="是否让该优惠券失效" confirmButtonText="失效"
                        cancelButtonText="取消" @confirm.stop="updateCouponStatus(scope.row)">
                        <template #reference>
                            <el-button type="danger" size="small">失效</el-button>
                        </template>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
        <div class="noticeBottom">
            <el-pagination background layout="prev,pager, next" :total="total" :current-page="currentPage"
                :page-size="limit" @current-change="getData" />
        </div>
        <FormDrawer ref="formDrawerRef" :title="drawerTitle" @submit="handleSubmit">
            <el-form :model="form" ref="formRef" :rules='rules' label-width="100px" :inline="false">
                <el-form-item label="优惠券名称" prop="name">
                    <el-input v-model="form.name" placeholder="请输入优惠券名称" style="width: 50%"></el-input>
                </el-form-item>
                <el-form-item label="类型" prop="type">
                    <el-radio-group v-model="form.type">
                        <el-radio :value="0">满减</el-radio>
                        <el-radio :value="1">折扣</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="面值" prop="value">
                    <el-input v-model="form.value" placeholder="面值" style="width: 50%">
                        <template #append>{{ form.type ? "折" : "元" }}</template>
                    </el-input>
                </el-form-item>
                <el-form-item label="发行量" prop="total">
                    <el-input-number v-model="form.total" :min="0" :max="10000">
                    </el-input-number>
                </el-form-item>
                <el-form-item label="最低使用价格" prop="min_price">
                    <el-input v-model="form.min_price" placeholder="请输入最低使用价格" type="number"></el-input>
                </el-form-item>
                <el-form-item label="排序" prop="order">
                    <el-input-number v-model="form.order" :min="0" :max="10000"></el-input-number>
                </el-form-item>
                <el-form-item label="活动时间">
                    <el-date-picker :editable="false" v-model="timerange" type="datetimerange"
                        value-format="YYYY-MM-DDTHH:mm:ss" range-separator="To" start-placeholder="开始时间"
                        end-placeholder="结束时间" />
                </el-form-item>
                <el-form-item label="描述" prop="desc">
                    <el-input v-model="form.desc" placeholder="请输入描述" type="textarea" :rows="5"></el-input>
                </el-form-item>
                <el-form-item label="公告内容" prop="content">
                    <el-input v-model="form.content" placeholder="请输入公告内容" type="textarea" :rows="5"></el-input>
                </el-form-item>
            </el-form>
        </FormDrawer>
    </el-card>
</template>
<script setup>
import { computed } from 'vue'
import ListHeader from '@/components/ListHeader.vue'
import FormDrawer from '@/components/FormDrawer.vue'
import { getCouponList, createCoupon, updateCoupon, deleteCoupon, updateCouponStatus } from '@/api/coupon'
import { useInitTable, useInitForm } from '@/hooks/useCommon'

function formatStatus(row) {
    let s = "领取中"
    let start_time = (new Date(row.start_time)).getTime()
    let now = (new Date()).getTime()
    let end_time = (new Date(row.end_time)).getTime()
    if (start_time > now) {
        s = "未开始"
    } else if (end_time < now) {
        s = "已结束"
    } else if (row.status = 0) {
        s = "已失效"
    }
    return s
}

// 获取、分页、删除
const {
    tableData,
    loading,
    currentPage,
    total,
    limit,
    getData,
    handleDelete,
    handleStatusChange
} = useInitTable({
    getList: getCouponList,
    onGetListSuccess: (res) => {
        tableData.value = res.list.map(o => {
            // 转化优惠卷状态
            o.statusText = formatStatus(o)
            return o
        })
        total.value = res.totalCount
    },
    delete: deleteCoupon,
    udpateStatus: updateCouponStatus 
})

// 新增、修改
const {
    formDrawerRef,
    formRef,
    rules,
    form,
    drawerTitle,
    handleSubmit,
    handleCreate,
    handleEdit
} = useInitForm({
    form: {
        name: "",
        type: 0,
        value: 0,
        total: 100,
        min_price: 0,
        start_time: null,
        end_time: null,
        order: 50,
        desc: ""
    },
    rules: {
        title: [{
            required: true,
            message: '公告标题不能为空',
            trigger: 'blur'
        }],
        content: [{
            required: true,
            message: '公告内容不能为空',
            trigger: 'blur'
        }]
    },
    getData,
    update: updateCoupon,
    create: createCoupon,
    beforeSubmit: (f) => {
        if (typeof f.start_time != "number") {
            f.start_time = (new Date(f.start_time)).getTime()
        }
        if (typeof f.end_time != "number") {
            f.end_time = (new Date(f.end_time)).getTime()
        }
        return f
    }
})

const timerange = computed({
    get() {
        return form.start_time && form.end_time ? [form.start_time, form.end_time] : []
    },
    set(val) {
        form.start_time = val[0]
        form.end_time = val[1]
    }
})

</script>
<style scoped lang="less">
.notice {
    border: 0;

    .active {
        border: 1px dashed red;
        background-color: rgb(220, 159, 159);
        color: red;
    }

    .inactive {
        border: 1px dashed #eee;
        background-color: #c9c9c9;
        color: #eee;
    }

    .couponName {
        border: 1px dashed #eee;
        padding: 2px 4px;
        border-radius: 5px;

        .couponNameH5 {
            font-weight: 800;
            font-size: 18px;
        }
    }

    .noticeBottom {
        display: flex;
        align-items: center;
        justify-content: center;
        margin-top: 10px;
    }
}
</style>